package com.sun.bata;

import java.util.ArrayList;
import java.util.Random;

import com.sun.lotter.R.string;

public class MakeLotterNumber {
	/**
	 * @param tempTime
	 *            :当前时间 <br>
	 * @param constellationId
	 *            :星座ID <br>
	 * @param name
	 *            :姓名 <br>
	 * @param lotterId
	 *            :彩种ID <br>
	 * @param playID
	 *            :玩法ID <br>
	 * @param luckID
	 *            :选号器ID
	 */
	public Random getRandom(java.util.Date tempTime, int constellationId,
			string name, int lotterId, int playId, int luckId) {
		long temp = 0;
		long _LuckID = 0;// 选号器ID 2位
		long _lotterID = 0;// 彩种ID 2位
		long _playID = 0;// 玩法ID 4位
		long _constellationId = 0; // 4位
		int _totalHours = 0;
		long _nameID = 0;// 姓名

		_LuckID = ((long) luckId << 0);// 选号器ID
		_lotterID = ((long) lotterId) << 2;
		_playID = ((long) playId) << 4;

		_totalHours = (int) ((tempTime.getTime()) / (1000 * 60 * 60));
		System.out.println(_totalHours);
		if (luckId == 1 || luckId == 2)// 星座,生肖
		{
			_constellationId = constellationId << 8;
			_totalHours = (_totalHours) << 12;
		} else if (luckId == 3)// 生日选号器
		{

			_totalHours = (_totalHours) << 8;
		} else if (luckId == 3)// 姓名选号器
		{
			_nameID = name.hashCode() << 6;
		}

		temp = _LuckID ^ _lotterID ^ (_playID) ^ _totalHours ^ _nameID
				^ _constellationId;
		System.out.println(_LuckID+","+_lotterID+","+_playID+","+_totalHours+","+_nameID+","+_constellationId+","+temp);
		Random rand = new Random(temp);
		return rand;
	}

	public ArrayList<String> makeLotterNumbers(int lotterID, int playID,
			int number) {
		Random rand = new Random();
		return makeLotterNumbers(rand, lotterID, playID, number);
	}

	/**
	 * �������ã�����������
	 * 
	 * @param lotterID
	 *            :彩种ID
	 * @param playID:玩法ID
	 * @param number:号码注数
	 * @return
	 */
	public ArrayList<String> makeLotterNumbers(Random rand, int lotterID,
			int playID, int number) {
		ArrayList<String> numbers = new ArrayList<String>();
		switch (lotterID) {
		case LotterData.DOUBLECOLORBALL:// 双色球
			for (int i = 0; i < number; i++) {
				StringBuffer ss = new StringBuffer();
				int temp[] = makeRandom(rand, 6, 1, 33);
				sort(temp);
				for (int j = 0; j < temp.length; j++) {
					ss.append(" " + (temp[j] > 9 ? temp[j] : "0" + temp[j]));
				}
				temp = makeRandom(rand, 1, 1, 16);
				for (int j = 0; j < temp.length; j++) {
					ss.append("-" + (temp[j] > 9 ? temp[j] : "0" + temp[j]));
				}
				numbers.add(ss.toString().trim());
			}

			break;
		case LotterData.FREQLOTT:// 时时彩
			for (int i = 0; i < number; i++) {
				StringBuffer ss = new StringBuffer();
				int temp[] = new int[1];
				switch (playID) {
				case 0:// �����淨
				case 1:// ����ͨѡ
					temp = MakeRandom2(rand, 5, 0, 9);
					for (int k = 0; k < temp.length; k++) {
						ss.append(temp[k] + ",");
					}
					ss.deleteCharAt(ss.length() - 1);
					break;
				case 2:// �����淨
					temp = MakeRandom2(rand, 3, 0, 9);
					ss.append("-,-,");
					for (int k = 0; k < temp.length; k++) {
						ss.append(temp[k] + ",");
					}
					ss.deleteCharAt(ss.length() - 1);
					break;
				case 3:// �����淨
					temp = MakeRandom2(rand, 2, 0, 9);

					ss.append("-,-,-,");
					for (int k = 0; k < temp.length; k++) {
						ss.append(temp[k] + ",");
					}
					ss.deleteCharAt(ss.length() - 1);
					break;
				case 4:// һ���淨
					temp = MakeRandom2(rand, 1, 0, 9);
					ss.append("-,-,-,-,");
					for (int k = 0; k < temp.length; k++) {
						ss.append(temp[k] + ",");
					}
					ss.deleteCharAt(ss.length() - 1);
					break;
				case 5:// ��С��˫
					temp = MakeRandom2(rand, 2, 1, 4);
					for (int k = 0; k < temp.length; k++) {

						switch (temp[k]) {
						case 1:
							ss.append("С" + " ");
							break;
						case 2:
							ss.append("��" + " ");
							break;
						case 3:
							ss.append("��" + " ");
							break;
						case 4:
							ss.append("˫" + " ");
							break;
						default:
							break;
						}
					}
					break;
				case 6:
					temp = makeRandom(rand, 2, 0, 9);
					for (int k = 0; k < temp.length; k++) {
						ss.append(temp[k] + ",");
					}
					ss.deleteCharAt(ss.length() - 1);
					break;
				}
				numbers.add(ss.toString().trim());
			}
			break;
		case LotterData.SELECT5IN11:// ����ѡ��
			for (int i = 0; i < number; i++) {
				StringBuffer ss = new StringBuffer();
				int temp[];
				switch (playID) {
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
					temp = makeRandom(rand, playID + 1, 1, 11);
					sort(temp);
					for (int k = 0; k < temp.length; k++) {
						ss.append(" " + (temp[k] > 9 ? temp[k] : "0" + temp[k]));
					}
					break;
				case 8:
				case 9:

					temp = makeRandom(rand, playID - 6, 1, 11);
					sort(temp);
					for (int k = 0; k < temp.length; k++) {
						ss.append(" " + (temp[k] > 9 ? temp[k] : "0" + temp[k]));
					}
					break;
				case 10:
				case 11:
				default:
					temp = makeRandom(rand, playID - 8, 1, 11);
					sort(temp);
					for (int k = 0; k < temp.length; k++) {
						ss.append("," + (temp[k] > 9 ? temp[k] : "0" + temp[k]));
					}
					ss.deleteCharAt(0);
					break;
				}

				numbers.add(ss.toString().trim());
			}
			break;
		case LotterData.SUPERHAPPY:// ����͸
			switch (playID) {
			case 0:// ��׼�淨
				for (int i = 0; i < number; i++) {
					StringBuffer ss = new StringBuffer();
					int temp[] = makeRandom(rand, 5, 1, 35);
					sort(temp);
					for (int j = 0; j < temp.length; j++) {
						ss.append(" " + (temp[j] > 9 ? temp[j] : "0" + temp[j]));
					}
					temp = makeRandom(rand, 2, 1, 12);
					sort(temp);
					ss.append("-");
					for (int j = 0; j < temp.length; j++) {
						ss.append((temp[j] > 9 ? temp[j] : "0" + temp[j]) + " ");
					}
					numbers.add(ss.toString().trim());
				}
				break;
			case 1:// ��Ф��
				for (int i = 0; i < number; i++) {
					StringBuffer ss = new StringBuffer();
					int temp[] = makeRandom(rand, 2, 1, 12);
					sort(temp);
					for (int j = 0; j < temp.length; j++) {
						ss.append(" " + (temp[j] > 9 ? temp[j] : "0" + temp[j]));
					}
					numbers.add(ss.toString().trim());
				}
				break;
			default:
				break;
			}

			break;
		case LotterData.WELFARE3D:// ���ʣ�D
			for (int i = 0; i < number; i++) {
				StringBuffer ss = new StringBuffer();
				int temp[] = MakeRandom2(rand, 3, 0, 9);
				for (int k = 0; k < temp.length; k++) {
					ss.append("," + temp[k]);
				}
				numbers.add(ss.deleteCharAt(0).toString());
			}
			break;
		}
		return numbers;
	}

	/**
	 * ������ num:����ͬ���� ���������ΪMax
	 */
	public static int[] makeRandom(Random rand, int num, int min, int max) {
		int a[] = new int[num];
		int b[];
		if (min < 0) {
			b = new int[max + 1 - min];
		} else
			b = new int[max + 1];
		for (int i = 0; i < num; i++) {
			a[i] = getRnd(rand, min, max);
			if (b[a[i]] > 0) {
				i--;
			} else {
				b[a[i]] = 1;
			}

		}
		return a;
	}

	/**
	 * ������num����ظ�������.
	 * 
	 * @param num
	 * @param max
	 * @return
	 */
	public static int[] MakeRandom2(Random rnd, int num, int min, int max) {
		int a[] = new int[num];
		for (int i = 0; i < num; i++) {
			a[i] = getRnd(rnd, min, max);
		}
		return a;
	}

	public static int getRnd(Random rnd, int min, int max) {
		return (rnd.nextInt() >>> 1) % (max - min + 1) + min;
	}

	// ��ϵ,��С����.
	public static void sort(int[] a) {
		int temp = 0;
		for (int i = 0; i < a.length - 1; i++) {
			for (int j = 0; j < a.length - i - 1; j++) {
				if (a[j] > a[j + 1]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}
}
